package com.example.JavaWebApi.model.vo.systemVo;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import lombok.Data;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;

@Data
public class ControllerDescription {
    protected String name;
    protected String description;
    protected String mapping;
    private List<ApiDescription> apiDescriptionList;
    protected String getUniqueId(){
        return "controller"+name+"noneMethod";
    }
    public String getId() throws NoSuchAlgorithmException {
        String path=getUniqueId();
        MessageDigest digest=MessageDigest.getInstance("SHA-256");
        byte[] hash=digest.digest(path.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb=new StringBuilder();
        for (byte b: hash){
            String hex=Integer.toHexString(0xff & b);
            if(hex.length()==1){
                sb.append("0");
            }
            sb.append(hex);
        }
        return sb.toString();
    }
    @Data
    public static class ApiDescription extends ControllerDescription{
        private String method;
        private String parent;
        @Override
        protected String getUniqueId() {
            return parent+name+method;
        }
    }
}
